// 日期转换
function js_date_time(unixtime) {
  var date = new Date(unixtime);
  var y = date.getFullYear();
  var m = date.getMonth() + 1;
  m = m < 10 ? "0" + m : m;
  var d = date.getDate();
  d = d < 10 ? "0" + d : d;
  var h = date.getHours();
  h = h < 10 ? "0" + h : h;
  var minute = date.getMinutes();
  var second = date.getSeconds();
  minute = minute < 10 ? "0" + minute : minute;
  second = second < 10 ? "0" + second : second;
  // return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;//年月日时分秒
  return y + "-" + m + "-" + d + " " + h + ":" + minute;
}

// 字符串分割
function getTags(tag) {
  var a = tag.split(";");
  return a;
}

// 字符截取
function stringChart(str) {
  if (str.length > 6) {
    return str.substring(0, 6);
  } else {
    return str;
  }
}

//米——千米
function distanceTransfom(distance) {
  return (Math.round(distance / 100) / 10).toFixed(1);
}

/**
 * 传入时间后几天
 * param：later:往后多少天
 */
function dateLater(later) {
  var date = new Date();
  date.setDate(date.getDate() + later);
  var day = date.getDay();
  var month =
    date.getMonth() + 1 < 10
      ? "0" + (date.getMonth() + 1)
      : date.getMonth() + 1;
  var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
  return month + "-" + day;
}

function getNowTime() {
  var now = new Date();
  var hour = now.getHours();
  var minute = now.getMinutes();
  var formatTime = hour + ":" + minute;
  return formatTime;
}

//把日期转化为时间戳数字
function timeTampTransform(time) {
  var dt = new Date(time.replace(/-/, "/"));
  return dt.getTime();
}

//屏蔽敏感词
function shieldWords(content) {
  let str = content.replace(/习近平|习大大/g, function(s) {
    let str = "";
    for (let i = 0; i < s.length; i++) {
      str += "*";
    }
    return str;
  });
}

/**
 * 计算两个经纬度的距离(千米)
 */
function getDistance(lat1, lng1, lat2, lng2) {
  var radLat1 = (lat1 * Math.PI) / 180.0;
  var radLat2 = (lat2 * Math.PI) / 180.0;
  var a = radLat1 - radLat2;
  var b = (lng1 * Math.PI) / 180.0 - (lng2 * Math.PI) / 180.0;
  var s =
    2 *
    Math.asin(
      Math.sqrt(
        Math.pow(Math.sin(a / 2), 2) +
          Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)
      )
    );
  s = s * 6378.137; // EARTH_RADIUS;
  s = Math.round(s * 10000) / 10000;
  return Number(s).toFixed(1);
}

const telStr = /^[1](([3][0-9])|([4][5-9])|([5][0-3,5-9])|([6][5,6])|([7][0-8])|([8][0-9])|([9][1,8,9]))[0-9]{8}$/;

module.exports = {
  js_date_time: js_date_time,
  getTags: getTags,
  stringChart: stringChart,
  distanceTransfom: distanceTransfom,
  dateLater: dateLater,
  getNowTime: getNowTime,
  timeTampTransform: timeTampTransform,
  shieldWords: shieldWords,
  getDistance: getDistance,
  telStr: telStr
};

